/*
 * AbstractMap.h
 *
 *  Created on: 05.12.2011
 *      Author: jan
 */

#ifndef ABSTRACTMAP_H_
#define ABSTRACTMAP_H_
#include <cmath>
#include <vector>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "Point.h"
#include "MathUtil.h"
#include <iostream>
#include <fstream>


namespace std {
template <class T>
class AbstractMap {
public:
	int height;
	int width;
	int picWidth;
	int picHeight;
	vector<vector<T> > data;
	IplImage * img;
	double mapvalue;

	AbstractMap(int height, int width, int picWidth,int picHeight):height(height),width(width),picWidth(picWidth),picHeight(picHeight),img(NULL),mapvalue(0){}

	virtual ~AbstractMap(){}
	virtual Point random_free_place() = 0;
	virtual bool is_in(Point p) = 0;
	virtual bool is_free(Point p) = 0;
	virtual double distance_to_nearest_beacon(Point p) = 0;
	Point random_place() {
		return Point(UNIFORM_RAND_WITHOUT_ONE * width,UNIFORM_RAND_WITHOUT_ONE * height);
	}
	void saveMap(string s){
		fstream filestr;
		filestr.open(s.c_str(),fstream::in|fstream::out|fstream::trunc|fstream::binary);
		filestr << height << width;
		for(unsigned int i = 0; i  < data.size();i++){
			for(unsigned int j = 0; j  < data[i].size();j++){
				filestr << data[i][j];
			}
		}
		filestr << endl;
		filestr.close();
	}
	void loadMap(string s) {
		fstream filestr;
		filestr.open(s.c_str(),	fstream::in | fstream::out | fstream::app | fstream::binary);
		filestr >> height >> width;
		this->data.resize(width, vector<T>());
		for (int j = 0; j < width; j++) {
			this->data[j].resize(height, T());
		}
		for (unsigned int i = 0; i < data.size(); i++) {
			for (unsigned int j = 0; j < data[i].size(); j++) {
				filestr >> data[i][j];
			}
		}
		filestr.close();
	}
};

} /* namespace std */
#endif /* ABSTRACTMAP_H_ */
